home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10278 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  50 lines

  1. Path: solon.com!not-for-mail
  2. From: Samuel Oosterhuis <soosterh@uoguelph.ca>
  3. Newsgroups: comp.lang.c,comp.lang.c.moderated,hp.unix,comp.sys.hp.apps,comp.sys.hp.hpux
  4. Subject: Re: C coding problem
  5. Date: 16 Mar 1996 09:20:11 -0600
  6. Organization: UoGuelph
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4iem7b$9u6@solutions.solon.com>
  10. References: <4ianbf$h86@solutions.solon.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12. X-Mailer: Mozilla 2.0 (Win95; I)
  13.  
  14. Stephen Proctor wrote:
  15. > I am trying to write a *simple* C program and am running into the following
  16. > warning problem shown below.  Why do I get this warning?[snippage]
  17. > main(int argc, char *argv[])
  18. > {
  19. > int ii;
  20. > int option_val = 0;
  21. > /* skip lines */
  22. > for (ii=1; ii<argc; ii++) {
  23. > /* skip lines */
  24. > /* The next line is the offending line */
  25. >    if (*argv[ii] == '-') option_val = read_options;
  26. > /* skip lines */
  27. > return 0;         /* Program executed successfully */
  28. > }
  29.  
  30. First of all I suggest that you read some of the K & R examples.
  31. There is a good example of dealing with this exact type of problem
  32. on page 117 of the second edition.  But for your immediate problem
  33. you might try rewriting your for loop as follows:
  34.  
  35.     /* loop once for each parameter */
  36.     while( --argc > 0 ){
  37.         /* check if the parameter starts with '-' */
  38.         if((*++argv)[0] == '-' )
  39.             option_val = read_options;
  40.         /* rest of the loop stuff */
  41.     }
  42.  
  43. In your program you were pointer at the string that contained the
  44. full parameter not just the first character of that string.
  45.  
  46. Samuel (soosterh@uoguelph.ca)
  47.  
  48. [Note that the example contains an error, which is that it increments
  49.  argv[n]. -mod]
  50.